package cn.shutdown.demo.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.shutdown.demo.service.DemoUserService;

@Controller
public class DemoLoginController {

	@Autowired
	private DemoUserService userService;

	@RequestMapping(value = "/demo/loginPage.do")
	public ModelAndView loginPage(Model model, HttpSession session,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		List<Object> list = new ArrayList<Object>();
		list.add(new BigInteger("1"));
		list.add(new BigInteger("6"));
		list.add(new BigInteger("1"));
		list.add(new BigInteger("0"));
		mav.addObject("list", list);
		List<Object> list1 = new ArrayList<Object>();
		list1.add(new Integer("1"));
		list1.add(new Integer("6"));
		list1.add(new Integer("1"));
		list1.add(new Integer("0"));
		mav.addObject("list1", list1);
		List<Object> list2 = new ArrayList<Object>();
		list2.add(1l);
		list2.add(6l);
		list2.add(1l);
		list2.add(0l);
		mav.addObject("list2", list2);
		session.removeAttribute("abcde");
		request.setAttribute("abcde", "abcde");
		return mav;
	}

	/**
	 * 从登录页面获取form表单的属性，然后判断并跳转
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/demo/login.do")
	public ModelAndView login(Model model,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "password") String password) {
		ModelAndView mav = new ModelAndView();
		boolean loginFlag = userService.hasMatchUser(userName, password);
		if (loginFlag) {
			mav.setViewName("demo/main");
			mav.addObject("message", "欢迎光临 " + userName);
		} else {
			mav.setViewName("demo/loginPage");
			mav.addObject("message", "登录失败");
		}
		return mav;
	}

	@RequestMapping(value = "/demo/registPage.do")
	public ModelAndView registPage(Model model) {
		ModelAndView mav = new ModelAndView();
		return mav;
	}

	@RequestMapping(value = "/demo/regist.do")
	public ModelAndView regist(Model model,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "password") String password) {
		userService.saveUser(userName, password);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("demo/main");
		mav.addObject("message", "欢迎光临 " + userName);
		return mav;
	}

	@RequestMapping(value = "/demo/ajaxTest.do")
	public ModelAndView regist(Model model) {
		ModelAndView mav = new ModelAndView();
		return mav;
	}

	@RequestMapping(value = "/demo/ajaxTest1.do")
	public @ResponseBody Map<String, String> ajaxTest1(Model model,
			@RequestParam(value = "username") String username,
			@RequestParam(value = "content") String content) throws UnsupportedEncodingException {
		//这个参数里面如果是中文的时候会是乱码的、这时候需要改tomcat的servers.xml,在端口上增加 URIEncoding="UTF-8" 属性
		//这样参数就不会乱码了
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "参数username的值为" + username
				+ "  参数content的值为" + content);
		return map;
	}
	
	@RequestMapping(value = "/demo/bootstrap.do")
	public ModelAndView bootstrap(Model model) {
		ModelAndView mav = new ModelAndView();
		return mav;
	}
	
	@RequestMapping(value = "/demo/testCss.do")
	public ModelAndView testCss(Model model){
		return new ModelAndView();
	}
}
